package com.emergency.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emergency.data.entity.Test;
import com.emergency.data.param.TestParam;
import com.emergency.data.vo.TestVO;
import com.emergency.mapper.TestMapper;
import com.emergency.service.TestService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Test)测试服务实现类
 *
 * @author makejava
 * @since 2020-09-17
 */
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {

    /**
     * 模拟测试数据的集合容器
     */
    private static List<Test> testList = new ArrayList<>();

    @Override
    public boolean insert(TestParam param) {
        try {
            Test test = new Test();
            BeanUtil.copyProperties(param, test);
            //生成id
            Snowflake snow = new Snowflake(2, 2);
            test.setId(snow.nextId());
            test.setAddTime(LocalDateTime.now());
            testList.add(test);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public List<TestVO> getData() {
        return testList.stream().map(c -> {
            TestVO testVO = new TestVO();
            BeanUtil.copyProperties(c, testVO);
            return testVO;
        }).collect(Collectors.toList());
    }
}
